$(function() {
    startPage(1); //初始化页面

    // 上一页
    $('#prePage').click(function() {
        var currentPage = $('#currentPage').text();
        if(currentPage > 1){
            startPage(--currentPage);
            console.log(currentPage);
        }
        else return;
    })
    // 下一页
    $('#nextPage').click(function() {
        var pages = $('#pages').text();
        var currentPage = $('#currentPage').text();
        if(pages > 0 && currentPage < pages) startPage(++currentPage);
        console.log(currentPage);
    })
    // 跳转指定页
    $('#goPage').click(function() {
        var page = $('#page').val();
        var pages = $('#pages').text();
        if(page != '' && page > 0 && page <= pages){
            startPage(page);
        }else{
            alert('找不到指定页');
        }
    })
    //标记所有消息为已读
    $('#signAll').click(function() {
        var content = $('#panel-list').html().trim(' ');
        if(content != '' && content.length > 0){
            layer.confirm('是否全部标记为已读?',{
                btn: ['确定', '取消']
            }, function() {
                $.ajax({
                    url: '/user/advices/comments/receive',
                    method: 'put',
                    contentType: 'application/json;charset=utf-8',
                    dataType: 'json',
                    success: function(data) {
                        if(data.code == 1001){
                            startPage(1);
                            layer.msg(data.message, {time: 2000});
                        }
                    },
                    error: function (err) {
                        alert('回调接口失败');
                    }
                })
            }, function() {
                return;
            });

        }
    })
    //清空所有消息记录
    $('#cleanAll').click(function() {
        var content = $('#panel-list').html().trim(' ');
        if(content != '' && content.length > 0){
            layer.confirm('是否清空所有消息?',{
                btn: ['确定', '取消']
            }, function() {
                $.ajax({
                    url: '/user/advices/comments/receive',
                    method: 'delete',
                    contentType: 'application/json;charset=utf-8',
                    dataType: 'json',
                    success: function(data) {
                        if(data.code == 1001){
                            $('#panel-list').html('');
                            layer.msg(data.message, {time: 2000});
                        }
                    },
                    error: function (err) {
                        alert('回调接口失败');
                    }
                })
            }, function() {
                return;
            });

        }
    })
})

/**
 * 请求分页消息
 * @param page
 */
function startPage(page) {
    if(page < 1) page = 1;
    $.ajax({
        url: '/user/advices/comments/receive?page=' + page,
        method: 'get',
        contentType: 'application/json;charset=utf-8',
        dataType: 'json',
        success: function(data) {
            if(data.code === "1001"){
                var list = data.data.list;
                console.log(list);
                var strHtml = '';
                for(var i=0;i<list.length;i++){
                    strHtml += '<div class="panel-item">';
                    strHtml += '<div class="left-item"><div class="head-img"><img src="' + list[i].fromUrl + '" alt="' + list[i].fromNickname + '" /></div></div>';
                    strHtml += '<div class="right-item">';
                    strHtml += '<div class="item-nickname"><a href="' + list[i].fromUserName + '">' + list[i].fromNickname + '</a>';
                    if(list[i].isRead === 1)
                        strHtml += '<div class="pull-right"><span class="isread"">已读</span>';
                    else
                        strHtml += '<div class="pull-right"><span class="isread unread">未读</span>';
                    strHtml += '<span class="glyphicon glyphicon-remove delread"></span></div></div>';
                    strHtml += '<div class="item-time">' + list[i].commentTime + '</div>';
                    if(list[i].type === 0)
                        strHtml += '<div class="item-content"><a href="/' + list[i].toUserName + '/article/details/' + list[i].parentId +  '#comments_list">对你的文章进行了评价：' + list[i].content + '</a></div></div></div>';
                    else
                        strHtml += '<div class="item-content"><a href="/' + list[i].toUserName + '/article/details/' + list[i].parentId +  '#comments_list">回复你：' + list[i].content + '</a></div></div></div>';
                }
                $('#panel-list').html(strHtml);
                //分页导航条
                $('#currentPage').text(data.data.current);
                $('#pages').text(data.data.pages);
                var firstIndex = data.data.firstIndex;
                //修改指定未读情况
                unRead(firstIndex);
                monitorContent(firstIndex);
                //删除指定消息
                delRead(firstIndex);
            }
        },
        error: function(err) {
            alert("回调函数失败");
        }
    })
}

function unRead(firstIndex) {
    $('.isread').each(function(index) {
        var el = $(this);
        var idx = firstIndex++;
        $(this).click(function() {
            if($(el).hasClass('unread'))
                $.ajax({
                    url: '/user/advices/comment/receive?index=' + idx,
                    method: 'put',
                    contentType: 'application/json;charset=utf-8',
                    dataType: 'json',
                    success: function(data) {
                        if(data.code === '1001'){
                            $(el).html('已读');
                            $(el).removeClass('unread');
                        }
                    },
                    error: function(err) {
                        alert("回调函数失败");
                    }
                })
        })
    })
}

function delRead(firstIndex) {
    $('.delread').each(function(index) {
        var el = $(this);
        var idx = firstIndex++;
        $(this).click(function () {
            console.log(idx);
            layer.confirm('是否删除该消息？',{
                btn: ['确定', '取消']
            },function() {
                $.ajax({
                    url: '/user/advices/comment/receive?index=' + idx,
                    method: 'delete',
                    contentType: 'application/json;charset=utf-8',
                    dataType: 'json',
                    success: function(data) {
                        if(data.code === 1001){
                            startPage(1);
                        }
                        layer.msg(data.message, {time: 2000});

                    },
                    error: function(err) {

                    }
                })
            },function () {
                return;
            });

        })
    })
}

/**
 * 点击文本后跳转且更新消息未读情况
 * @param firstIndex
 */
function monitorContent(firstIndex) {
    $('.item-content').each(function(index) {
        var el = $(this);
        var idx = firstIndex++;
        $(this).click(function() {
            $.ajax({
                url: '/user/advices/comment/receive?index=' + idx,
                method: 'put',
                contentType: 'application/json;charset=utf-8',
                dataType: 'json',
                success: function(data) {
                }
            })
        })
    })
}